cmake_minimum_required(VERSION 3.10)
project(YinmeiPrinter VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -DNDEBUG")

find_package(Threads REQUIRED)

set(FILESYSTEM_LIB "stdc++fs")

include_directories(${CMAKE_CURRENT_SOURCE_DIR})

set(YINMEI_SOURCES
    yinmei.cpp
    yinmei.h
)

add_library(yinmei_static STATIC ${YINMEI_SOURCES})
target_link_libraries(yinmei_static Threads::Threads ${FILESYSTEM_LIB})

add_library(yinmei_shared SHARED ${YINMEI_SOURCES})
target_link_libraries(yinmei_shared Threads::Threads ${FILESYSTEM_LIB})
set_target_properties(yinmei_shared PROPERTIES OUTPUT_NAME yinmei)

add_executable(yinmei_demo main.cpp)
target_link_libraries(yinmei_demo yinmei_static Threads::Threads ${FILESYSTEM_LIB})

install(TARGETS yinmei_static yinmei_shared yinmei_demo
    ARCHIVE DESTINATION lib
    LIBRARY DESTINATION lib
    RUNTIME DESTINATION bin
)

install(FILES yinmei.h DESTINATION include)

set_target_properties(yinmei_demo PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)

enable_testing()

add_test(NAME yinmei_basic_test 
         COMMAND yinmei_demo
         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})

add_custom_target(clean_outputs
    COMMAND ${CMAKE_COMMAND} -E remove -f *.bin
    COMMENT "Cleaning output binary files"
)

message(STATUS "YinMei Printer Configuration:")
message(STATUS "  Build Type: ${CMAKE_BUILD_TYPE}")
message(STATUS "  C++ Standard: ${CMAKE_CXX_STANDARD}")
message(STATUS "  C++ Compiler: ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
message(STATUS "  Filesystem Library: ${FILESYSTEM_LIB}")
message(STATUS "  Source Directory: ${CMAKE_CURRENT_SOURCE_DIR}")
message(STATUS "  Binary Directory: ${CMAKE_CURRENT_BINARY_DIR}")

if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/img2prn")
    message(STATUS "img2prn found")
else()
    message(WARNING "img2prn not found in source directory")
endif()

if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/lpusb")
    message(STATUS "lpusb found")
else()
    message(WARNING "lpusb not found in source directory")
endif()

if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/libinkjet1lite.so")
    message(STATUS "libinkjet1lite.so found")
else()
    message(WARNING "libinkjet1lite.so not found in source directory")
endif()

add_custom_command(
    TARGET yinmei_demo POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        ${CMAKE_CURRENT_SOURCE_DIR}/img2prn
        ${CMAKE_CURRENT_SOURCE_DIR}/lpusb
        ${CMAKE_CURRENT_SOURCE_DIR}/libinkjet1lite.so
        ${CMAKE_CURRENT_SOURCE_DIR}/test.jpg
        $<TARGET_FILE_DIR:yinmei_demo>
    COMMENT "Copying required files to build directory"
)